package com.desksoft.ecommerce.helper.mall;

import java.util.ArrayList;
import java.util.List;

import com.desksoft.ecommerce.vo.mall.CateBrandVO;
import com.desksoft.ecommerce.vo.mall.CategoryVO;
import com.desksoft.ecommerce.vo.mall.ItemCharacterDVO;
import com.desksoft.ecommerce.vo.mall.PriceCharacterVO;
import com.desksoft.ecommerce.vo.mall.SearchItemsVO;
import com.desksoft.utility.cache.ICache;

/***************************************
 * <pre>
 * 版权所有：杭州迪科软件 保留所有权利
 * 创建日期: 2012-9-11 上午10:27:09
 * 创建作者：rq
 * 文件名称：SearchItemHelper.java
 * 版本：
 * 功能：
 * 最后修改时间：2012-9-11 上午10:27:09
 * 修改记录：
 * </pre>
 *****************************************/
public class SearchItemHelper {

	private static int DDMAXINDEX = 7;
	private static int DLMAXINDEX = 3;

	public static final String PRODUCT_LIST_MARK = "productlist";
	public static final String PROMOTION_LIST_MARK = "promotionlist";
	public static final String VENDOR_LIST_MARK = "vendorlist";

	private List<SearchItemDatas> list = new ArrayList<SearchItemDatas>();

	public static Object obj = new Object(); //TODO 去掉同步 去掉后台组装,用接口+各个实现类

	public void init(int ddindex) {
		init(ddindex, 3);
	}

	public void init(int ddindex, int dlindex) {
		DDMAXINDEX = ddindex;
		DLMAXINDEX = dlindex;
	}

	public void addCateData(String title, int dlindex, int ddindex, List<CategoryVO> catelist) {
		if (null != catelist && catelist.size() > 0) {
			SearchItemDatas se = new SearchItemDatas();
			se.setTitle(title);
			se.setDdindex(ddindex);
			se.setDlindex(dlindex);
			se.setType(SearchItemDataHelper.CATE_TYPE);
			if (list.size() == DLMAXINDEX) {
				se.setDlmoreStart(true);
				se.setDlmore(true);
			} else {
				se.setDlmoreStart(false);
				se.setDlmore(false);
			}
			se.setDlmoreEnd(false);
			int index = 1;
			int dlin = 1;
			for (CategoryVO vo : catelist) {
				SearchItemData sd = new SearchItemData();
				sd.setName(vo.getCategoryname());
				sd.setNum(String.valueOf(vo.getProductCount()));
				sd.setCode(SearchItemDataHelper.CATE_TYPE + SearchItemDataHelper.idSign + vo.getCategorycode()
						+ SearchItemDataHelper.fgsign + vo.getClazz() + SearchItemDataHelper.fgsign + vo.getFather());
				if (index > DDMAXINDEX) {
					if (dlin == 1) {
						sd.setIsStart(true);
						sd.setIsEnd(false);
					} else if (dlin == DDMAXINDEX || index == catelist.size()) {
						sd.setIsStart(false);
						sd.setIsEnd(true);
					} else {
						sd.setIsStart(false);
						sd.setIsEnd(false);
					}
					dlin++;
					if (dlin > DDMAXINDEX) {
						dlin = 1;
					}
					List<SearchItemData> li = se.getOthersub();
					if (null == li) {
						li = new ArrayList<SearchItemData>();
					}
					li.add(sd);
					se.setDdmore(true);
					se.setOthersub(li);
				} else {
					List<SearchItemData> li = se.getSub();
					if (null == li) {
						li = new ArrayList<SearchItemData>();
					}
					li.add(sd);
					se.setDdmore(false);
					se.setSub(li);
				}
				index++;
			}

			list.add(se);
		}
	}

	public void addBrandData(String title, int dlindex, int ddindex, List<CateBrandVO> brandlist) {
		if (null != brandlist && brandlist.size() > 0) {
			SearchItemDatas se = new SearchItemDatas();
			se.setTitle(title);
			se.setDdindex(ddindex);
			se.setDlindex(dlindex);
			se.setType(SearchItemDataHelper.BRAND_TYPE);
			if (list.size() == DLMAXINDEX) {
				se.setDlmoreStart(true);
				se.setDlmore(true);
			} else {
				se.setDlmoreStart(false);
				se.setDlmore(false);
			}
			se.setDlmoreEnd(false);
			int index = 1;
			int dlin = 1;
			for (CateBrandVO vo : brandlist) {
				SearchItemData sd = new SearchItemData();
				sd.setName(vo.getBrand());
				sd.setNum(String.valueOf(vo.getProductCount()));
				sd.setCode(SearchItemDataHelper.BRAND_TYPE + SearchItemDataHelper.idSign + vo.getBrandID()
						+ SearchItemDataHelper.fgsign + (vo.getCategoryD() == null ? "" : vo.getCategoryD()));
				if (index > DDMAXINDEX) {
					if (dlin == 1) {
						sd.setIsStart(true);
						sd.setIsEnd(false);
					} else if (dlin == DDMAXINDEX || index == brandlist.size()) {
						sd.setIsStart(false);
						sd.setIsEnd(true);
					} else {
						sd.setIsStart(false);
						sd.setIsEnd(false);
					}
					dlin++;
					if (dlin > DDMAXINDEX) {
						dlin = 1;
					}
					List<SearchItemData> li = se.getOthersub();
					if (null == li) {
						li = new ArrayList<SearchItemData>();
					}
					li.add(sd);
					se.setDdmore(true);
					se.setOthersub(li);
				} else {
					List<SearchItemData> li = se.getSub();
					if (null == li) {
						li = new ArrayList<SearchItemData>();
					}
					li.add(sd);
					se.setDdmore(false);
					se.setSub(li);
				}
				index++;
			}

			list.add(se);
		}
	}

	public void addPriceData(String title, int dlindex, int ddindex, List<PriceCharacterVO> priceList) {
		if (null != priceList && priceList.size() > 0) {
			SearchItemDatas se = new SearchItemDatas();
			se.setTitle(title);
			se.setDdindex(ddindex);
			se.setDlindex(dlindex);
			se.setType(SearchItemDataHelper.PRICE_TYPE);
			if (list.size() == DLMAXINDEX) {
				se.setDlmoreStart(true);
				se.setDlmore(true);
			} else {
				se.setDlmoreStart(false);
				se.setDlmore(false);
			}
			se.setDlmoreEnd(false);
			int index = 1;
			int dlin = 1;
			for (PriceCharacterVO vo : priceList) {
				SearchItemData sd = new SearchItemData();
				sd.setName(vo.getDescmeanings());
				sd.setNum(String.valueOf(vo.getProductCount()));
				sd.setCode(SearchItemDataHelper.PRICE_TYPE + SearchItemDataHelper.idSign + vo.getDescseq()
						+ SearchItemDataHelper.fgsign + (vo.getCategoryCode() == null ? "" : vo.getCategoryCode()));
				if (index > DDMAXINDEX) {
					if (dlin == 1) {
						sd.setIsStart(true);
						sd.setIsEnd(false);
					} else if (dlin == DDMAXINDEX || index == priceList.size()) {
						sd.setIsStart(false);
						sd.setIsEnd(true);
					} else {
						sd.setIsStart(false);
						sd.setIsEnd(false);
					}
					dlin++;
					if (dlin > DDMAXINDEX) {
						dlin = 1;
					}
					List<SearchItemData> li = se.getOthersub();
					if (null == li) {
						li = new ArrayList<SearchItemData>();
					}
					li.add(sd);
					se.setDdmore(true);
					se.setOthersub(li);
				} else {
					List<SearchItemData> li = se.getSub();
					if (null == li) {
						li = new ArrayList<SearchItemData>();
					}
					li.add(sd);
					se.setDdmore(false);
					se.setSub(li);
				}
				index++;
			}

			list.add(se);
		}
	}

	public void addDescData(String title, int dlindex, int ddindex, List<ItemCharacterDVO> desclist) {
		if (null != desclist && desclist.size() > 0) {
			SearchItemDatas se = new SearchItemDatas();
			se.setTitle(title);
			se.setDdindex(ddindex);
			se.setDlindex(dlindex);
			se.setType(SearchItemDataHelper.DESC_TYPE);
			if (list.size() == DLMAXINDEX) {
				se.setDlmoreStart(true);
				se.setDlmore(true);
			} else {
				se.setDlmoreStart(false);
				se.setDlmore(false);
			}
			se.setDlmoreEnd(false);
			int index = 1;
			int dlin = 1;
			for (ItemCharacterDVO vo : desclist) {
				SearchItemData sd = new SearchItemData();
				sd.setName(vo.getDescmeanings());
				sd.setNum(String.valueOf(vo.getProductCount()));
				sd.setCode(SearchItemDataHelper.DESC_TYPE + SearchItemDataHelper.idSign + vo.getDescseq()
						+ SearchItemDataHelper.fgsign + (vo.getCategoryCode() == null ? "" : vo.getCategoryCode()));
				if (index > DDMAXINDEX) {
					if (dlin == 1) {
						sd.setIsStart(true);
						sd.setIsEnd(false);
					} else if (dlin == DDMAXINDEX || index == desclist.size()) {
						sd.setIsStart(false);
						sd.setIsEnd(true);
					} else {
						sd.setIsStart(false);
						sd.setIsEnd(false);
					}
					dlin++;
					if (dlin > DDMAXINDEX) {
						dlin = 1;
					}
					List<SearchItemData> li = se.getOthersub();
					if (null == li) {
						li = new ArrayList<SearchItemData>();
					}
					li.add(sd);
					se.setDdmore(true);
					se.setOthersub(li);
				} else {
					List<SearchItemData> li = se.getSub();
					if (null == li) {
						li = new ArrayList<SearchItemData>();
					}
					li.add(sd);
					se.setDdmore(false);
					se.setSub(li);
				}
				index++;
			}

			list.add(se);
		}
	}

	public List<SearchItemDatas> getSearchItemData() {
		if (list.size() > DLMAXINDEX)
			if (null != list && list.size() > 0) {
				list.get(list.size() - 1).setDlmoreEnd(true);
			}
		return list;
	}

	/**
	 * 保存MANAGER
	 *
	 * @param cookieUuid
	 * @param manager
	 */
	private static void saveManager(String cookieUuid, String name, SearchItemsManager searchItemManager, ICache cache) {
		synchronized (obj) {
			searchItemManager.getSearchItemsVO().setCookieUuid(cookieUuid);
			cache.set(cookieUuid + SearchItemDataHelper.fgsign + name, searchItemManager.getSearchItemsVO());
		}
	}

	/**
	 * 保存Product MANAGER
	 *
	 * @param cookieUuid
	 * @param manager
	 */
	public static void saveProductManager(String cookieUuid, SearchItemsManager searchItemManager, ICache cache) {
		saveManager(cookieUuid, PRODUCT_LIST_MARK, searchItemManager, cache);
	}

	/**
	 * 保存Promotion MANAGER
	 *
	 * @param cookieUuid
	 * @param manager
	 */
	public static void savePromotionManager(String cookieUuid, SearchItemsManager searchItemManager, ICache cache) {
		saveManager(cookieUuid, PROMOTION_LIST_MARK, searchItemManager, cache);
	}

	/**
	 * 保存VendorMANAGER
	 *
	 * @param cookieUuid
	 * @param manager
	 */
	public static void saveVendorManager(String cookieUuid, SearchItemsManager searchItemManager, ICache cache) {
		saveManager(cookieUuid, VENDOR_LIST_MARK, searchItemManager, cache);
	}

	/**
	 * 获取MANAGER
	 *
	 * @param cookieUuid
	 * @return
	 */
	public static SearchItemsManager getProductManager(String cookieUuid, ICache cache) {
		return getManager(cookieUuid, PRODUCT_LIST_MARK, cache);
	}

	/**
	 * 获取MANAGER
	 *
	 * @param cookieUuid
	 * @return
	 */
	public static SearchItemsManager getPromotionManager(String cookieUuid, ICache cache) {
		return getManager(cookieUuid, PROMOTION_LIST_MARK, cache);
	}

	/**
	 * 获取MANAGER
	 *
	 * @param cookieUuid
	 * @return
	 */
	public static SearchItemsManager getVendorManager(String cookieUuid, ICache cache) {
		return getManager(cookieUuid, VENDOR_LIST_MARK, cache);
	}

	/**
	 * 获取MANAGER
	 *
	 * @param cookieUuid
	 * @return
	 */
	private static SearchItemsManager getManager(String cookieUuid, String name, ICache cache) {
		synchronized (obj) {
			SearchItemsVO vo = (SearchItemsVO) cache.get(cookieUuid + SearchItemDataHelper.fgsign + name);
			if (null == vo) {
				vo = new SearchItemsVO();
			}
			return new SearchItemsManager(vo);
		}
	}

	/**
	 * 删除MANAGER
	 *
	 * @param cookieUuid
	 * @return
	 */
	private static void delManager(String cookieUuid, String name, ICache cache) {
		synchronized (obj) {
			cache.delete(cookieUuid + SearchItemDataHelper.fgsign + name);
		}
	}
	/**
	 * 删除 Product MANAGER
	 *
	 * @param cookieUuid
	 * @return
	 */
	public static void delProductManager(String cookieUuid, ICache cache) {
		delManager(cookieUuid, PRODUCT_LIST_MARK, cache);
	}
	/**
	 * 删除 Promotion MANAGER
	 *
	 * @param cookieUuid
	 * @return
	 */
	public static void delPromotionManager(String cookieUuid, ICache cache) {
		delManager(cookieUuid, PROMOTION_LIST_MARK, cache);
	}
	/**
	 * 删除 Vendor MANAGER
	 *
	 * @param cookieUuid
	 * @return
	 */
	public static void delVendorManager(String cookieUuid, ICache cache) {
		delManager(cookieUuid, VENDOR_LIST_MARK, cache);
	}

}
